<template>
  <div>
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item
        v-for="(img, index) in bannerList"
        :key="index"
      >
        <img :src="img.imageUrl" width="100%" alt="">
      </van-swipe-item>
    </van-swipe>
    <div class="songlist-content">
      <div class="line">推荐歌单</div>
      <div
        v-for="(item, index) in songList"
        :key="index"
        class="songList"
        @click="goSonglist(item.id)"
      >
        <img :src="item.picUrl" width="118px" height="118px">
        <p class="song-title">{{item.name}}</p>
      </div>
    </div>
    <div>
      <div class="line">最新音乐</div>
      <div
        v-for="(item, index) in newSongList"
        :key="index"
        class="song-div"
      >
        <!-- <p class="song-name">{{item.name}}</p> -->
        <p class="song-name">
          <router-link :to="'/play/'+item.id">{{item.name}}</router-link></p>
        <p class="artist-name">
          <i class="sq"></i>
          <span
            v-for="(artist, idx) in item.song.artists"
            :key="'artist'+idx"
          >
            {{artist.name}}
            <!-- 歌手是多人时，添加分割线 -->
            <span v-if="item.song.artists.length>1 && idx!= item.song.artists.length-1">/</span>
          </span>
        </p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 轮播图
      bannerList: [],
      // 歌单
      songList: [],
      // 最新音乐
      newSongList: []
    }
  },
  created() {
    // 获取轮播图
    if(localStorage.getItem('banner')) {
      this.bannerList = JSON.parse(localStorage.getItem('banner'));
    }else {
      this.getBanner();
    }
    this.getSongList();
    this.getNewSong();
  },
  methods: {
    // 轮播图
    getBanner() {
      this.$http('/banner')
        .then(res => {
          this.bannerList = res.data.banners;
          // 保存到localStorage中
          localStorage.setItem('banner', JSON.stringify(this.bannerList));
        })
        .catch(err => {
          console.log(err);
        })
    },
    // 获取歌单
    getSongList() {
      this.$http('/personalized?limit=6')
        .then(res => {
          this.songList = res.data.result;
        })
        .catch(err => {
          console.log(err);
        })
    },
    // 获取最新音乐
    getNewSong() {
      this.$http('/personalized/newsong')
        .then(res => {
          this.newSongList = res.data.result;
        })
        .catch(err => {
          console.log(err);
        })
    },
    // 跳转到歌单详情页
    goSonglist(id) {
      this.$router.push('/songlist/'+id);
    }
  },
}
</script>

<style scoped>
 .my-swipe .van-swipe-item {
    color: #fff;
    font-size: 20px;
    /* line-height: 150px; */
    text-align: center;
    /* background-color: #39a9ed; */
  }
  .line {
    height: 20px;
    line-height: 20px;
    border-left: 2px solid #f00;
    margin-top: 10px;
    padding-left: 10px;
    font-size: 18px;
  }
  .songlist-content {
    min-height: 400px;
  }
  .songList {
    float: left;
    width: 28%;
    margin: 10px 8px;
  }
  .song-div {
    border-bottom: 1px solid #f4f4f4;
    padding-left: 10px;
  }
  .song-title {
    margin: 0;
    font-size: 14px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  .song-name {
    font-size: 16px;
  }
  .artist-name {
    font-size: 12px;
    color: #666;
  }
  .sq {
    display: inline-block;
    width: 14px;
    height: 10px;
    background-image: url('../../static/img/index_icon_2x.png');
    background-size: 186.75px 109.125px;
    background-position: 0 0;
  }
</style>